package com.jnu.mydiary.ui.daka;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.jnu.mydiary.AddActivity;
import com.jnu.mydiary.R;
import com.jnu.mydiary.calendar.RegistrationAdapter;
import com.jnu.mydiary.calendar.SpecialCalendar;
import com.jnu.mydiary.databinding.FragmentNotificationsBinding;
import com.jnu.mydiary.dialog.MoodEntity;
import com.jnu.mydiary.dialog.SelectMoodDialog;
import com.jnu.mydiary.ui.shouye1.myBean;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;

public class NotificationsFragment extends Fragment {

    private NotificationsViewModel notificationsViewModel;
    private FragmentNotificationsBinding binding;
    private GridView registration_calendar_gv;
    private TextView todayYear;
    private TextView todayMonth;
    private ImageView imageView;
    private RegistrationAdapter adapter;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
    SelectMoodDialog selectMoodDialog;
    int mYear=0;//年
    int mMonth=0;//月
    int mDay=0;//日
    private String selct_mood;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);

        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        registration_calendar_gv=(GridView)getActivity().findViewById(R.id.registration_calendar_gv);
        todayYear=(TextView)getActivity().findViewById(R.id.todayyear);
        todayMonth=(TextView)getActivity().findViewById(R.id.todaymonth);
        imageView=(ImageView)getActivity().findViewById(R.id.imageView2);


        Calendar calendar=Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR); // 获取当前年份
        mMonth = calendar.get(Calendar.MONTH) ;// 获取当前月份以（0开头）
        mDay = calendar.get(Calendar.DAY_OF_MONTH) ;// 获取当前天以（0开头）

        SpecialCalendar mCalendar=new SpecialCalendar();
        boolean isLeapYear =mCalendar.isLeapYear(mYear);
        int mDays=mCalendar.getDaysOfMonth(isLeapYear,mMonth+1);
        int week =mCalendar.getWeekdayOfMonth(mYear,mMonth);

        adapter=new RegistrationAdapter(this.getContext(),mDays,week,mDay);
        Bundle bundle = getActivity().getIntent().getExtras();//在这里去处理传过来的数据
        if(bundle!=null) {
            Serializable serializable = bundle.getSerializable("DataInfo");
            if (serializable != null && serializable instanceof myBean) {
                //goodsEntityList.add(new Data("111",20.0,"none",new Date(),R.drawable.home));
                myBean g = (myBean) serializable;
                if(bundle.getString("type").equals("add"))
                {
                    Log.i("type",bundle.getString("type"));
                    selct_mood=bundle.getString("mood");
                    int now_day=bundle.getInt("now_day");
                    adapter.setSelect_mood(selct_mood);
                    adapter.setNow_day(now_day);
                }
            }
        }

        binding.registrationCalendarGv.setAdapter(adapter);
        binding.registrationCalendarGv.setOnItemClickListener(this::onItemClick);
        Objects.requireNonNull(binding.todayyear).setText(mYear+"年");
        Objects.requireNonNull(binding.todaymonth).setText((mMonth+1)+"月");


        return root;
    }


//    @Override

    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        String today=mYear+"-"+mMonth+"-"+l;
        if (l!=0){
            TextView today_tv= (TextView) view.findViewById(R.id.day);
            ImageView imageView=(ImageView)view.findViewById(R.id.imageView2);

            today_tv.setTextColor(Color.BLACK);
            today_tv.setText(l+"");

            List<MoodEntity> list = new ArrayList<MoodEntity>();
            MoodEntity moodEntity = new MoodEntity("Happy",R.drawable.mood_kaixin,false);
            list.add(moodEntity);
            MoodEntity insurBean1 = new MoodEntity("Shy",R.drawable.mood_haixiu,false);
            list.add(insurBean1);
            MoodEntity insurBean2 = new MoodEntity("Cry",R.drawable.mood_ku,false);
            list.add(insurBean2);
            MoodEntity insurBean3 = new MoodEntity("Shock",R.drawable.mood_jingdaile,false);
            list.add(insurBean3);
            MoodEntity insurBean4 = new MoodEntity("Sleepy",R.drawable.mood_kun,false);
            list.add(insurBean4);
            MoodEntity insurBean5 = new MoodEntity("Joy",R.drawable.mood_kuaile,false);
            list.add(insurBean5);
            MoodEntity insurBean6 = new MoodEntity("Sick",R.drawable.mood_nanshou,false);
            list.add(insurBean6);
            MoodEntity insurBean7 = new MoodEntity("Angry",R.drawable.mood_shengqi,false);
            list.add(insurBean7);
            MoodEntity insurBean8 = new MoodEntity("Bald",R.drawable.mood_toutu,false);
            list.add(insurBean8);
            MoodEntity insurBean9 = new MoodEntity("Sorrow",R.drawable.mood_weiqu,false);
            list.add(insurBean9);
            MoodEntity insurBean10 = new MoodEntity("NoBB",R.drawable.mood_wuyu,false);
            list.add(insurBean10);
            MoodEntity insurBean11 = new MoodEntity("Soso",R.drawable.mood_yiban,false);
            list.add(insurBean11);



            selectMoodDialog = new SelectMoodDialog(getContext(), R.style.mall_Dialog);
            selectMoodDialog.setList(list);
            selectMoodDialog.setCanceledOnTouchOutside(true);
            selectMoodDialog.setDialogCallback(new SelectMoodDialog.DialogCallback() {
                @Override
                public void onClickRadioButton(String name,int pictureId) {
                    imageView.setImageResource(pictureId);
                    Toast.makeText(getActivity(),"你今天的心情的是："+name, Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(getActivity(), AddActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putInt("id_iv_mood",pictureId);
                    String day=today_tv.getText().toString();
                    String date=mYear+"-"+(mMonth+1)+"-"+day;
                    bundle.putString("date",date);
                    bundle.putString("type","daka");
                    bundle.putString("mood",name);
                    intent.putExtras(bundle);
                    startActivityForResult(intent,3);
                    selectMoodDialog.dismiss();

                }
            });

            selectMoodDialog.show();

            //imageView.setBackgroundResource(R.drawable.ic_baseline_download_done_24);
            view.setBackgroundColor(Color.TRANSPARENT);
            //Toast.makeText(view.getContext(),"您选择的日期："+today+"签到成功",Toast.LENGTH_SHORT).show();

        }



    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 3) {
            if (resultCode == 4) {

            }
        }
    }
}